<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入姓名"
          clearable
        />
      </el-form-item>
	  <el-form-item label="手机号" prop="phone">
	    <el-input
	      v-model="queryParams.phone"
	      placeholder="请输入手机号"
	      clearable
	    />
	  </el-form-item>
	  <el-form-item label="住户类型" prop="userType">
	    <el-select v-model="queryParams.userType"
	     class="width100"
	     placeholder="住户类型" clearable>
	      <el-option
	        v-for="item in peopleUserType"
	        :key="item.type"
	        :label="item.name"
	        :value="item.type"
	      />
	    </el-select>
	  </el-form-item>
	  <el-form-item label="楼宇" prop="floorId">
	    <el-select v-model="queryParams.floorId"
	     class="width100"
	     placeholder="楼宇" clearable>
	      <el-option
	       v-for="item in ListFloorArr"
	       :key="item.id"
	       :label="item.floorName"
	       :value="item.id"
	      />
	    </el-select>
	  </el-form-item>
	  <el-form-item label="居住房屋" prop="roomId">
	    <el-select v-model="queryParams.roomId"
	     class="width100"
	     placeholder="居住房屋" clearable>
	      <el-option
	        v-for="item in RoomList"
	        :key="item.id"
	        :label="item.communityName +''+ item.floorName +''+ item.units +'单元'+ item.roomNo +'号'"
	        :value="item.id"
	      />
	    </el-select>
	  </el-form-item>
	  <el-form-item label="用户状态" prop="userStatus">
	   <el-select v-model="queryParams.userStatus"
	    class="width100"
	    placeholder="审核状态" clearable>
	     <el-option
	       v-for="item in peopleUserStatus"
	       :key="item.type"
	       :label="item.name"
	       :value="item.type"
	     />
	   </el-select>
	  </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:post:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList" 
		
	> 
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="姓名" align="center" prop="userName" />
      <el-table-column label="房屋" align="center" prop="room" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="身份证号" align="center" prop="idCard" width="200" />
      <el-table-column label="住户类型" align="center" prop="userTypeCn" />
      <el-table-column label="审核状态" align="center" prop="userStatusCn" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:post:edit']"
          >修改</el-button>
		  <el-button
		    size="mini"
			v-if='scope.row.userStatus == 3 || scope.row.userStatus == 2'
		    type="text"
		    icon="el-icon-edit"
		    @click="handleStatusUpdate(scope.row)"
		    v-hasPermi="['system:post:edit']"
		  >审核</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:post:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改用户对话框 -->
	<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
	  <el-form ref="form" :model="form" :rules="rules" label-width="100px">
		  <el-form-item label="住户姓名" prop="userName">
		    <el-input placeholder="请输入住户姓名" v-model="form.userName" 
			 :disabled='userStatusFrom.userId'
			>
		    </el-input>
		  </el-form-item>
		<el-form-item label="住户手机号" prop="phone">
		  <el-input placeholder="请输入住户姓名" v-model="form.phone" :disabled='userStatusFrom.userId'>
		  </el-input>
		</el-form-item>
		<el-form-item label="住户身份证" prop="idCard">
		  <el-input placeholder="请输入住户身份证"  v-model="form.idCard" :disabled='userStatusFrom.userId'></el-input>
		</el-form-item>
	    <el-form-item label="居住房间" prop="roomId">
	      <el-select v-model="form.roomId"
		  :disabled='userStatusFrom.userId'
	       class="width100"
	       placeholder="居住房间" clearable>
	        <el-option
	         v-for="item in RoomList"
	         :key="item.id"
	         :label="item.communityName +''+ item.floorName +'栋'+ item.units +'单元'+ item.roomNo +'号'"
	         :value="item.id"
	        />
	      </el-select>
	    </el-form-item>
		<el-form-item label="住户类型" prop="userType">
		  <el-select v-model="form.userType"
		  :disabled='userStatusFrom.userId'
		   class="width100"
		   placeholder="住户类型" clearable>
		    <el-option
		     v-for="item in peopleUserType"
		     :key="item.type"
		     :label="item.name"
		     :value="item.type+''"
		    />
		  </el-select>
		</el-form-item>
		<el-form-item label="住户状态" prop="userStatus">
		  <el-select v-model="form.userStatus"
		   class="width100"
		   :disabled='userStatusFrom.userId'
		   placeholder="住户状态" clearable>
		    <el-option
		     v-for="item in peopleUserStatus"
		     :key="item.type"
		     :label="item.name"
		     :value="item.type+''"
		    />
		  </el-select>
		</el-form-item>
		<el-form-item label="住户审核" v-if='userStatusFrom.userId'>
		  <el-radio-group v-model="userStatusFrom.userStatus">
		    <el-radio  :label="0">审核通过</el-radio>
		    <el-radio  :label="3">审核拒绝</el-radio>
		  </el-radio-group>
		</el-form-item>
	  </el-form>
	  <div slot="footer" class="dialog-footer">
	    <el-button type="primary" @click="submitForm">确 定</el-button>
	    <el-button @click="cancel">取 消</el-button>
	  </div>
	</el-dialog>
  </div>
</template>

<script>
import { listUser, delUser, insertUser, editUser, allListUser, Examine} from "@/api/people/index";
import { allListFloor } from "@/api/house/buildmanagement";
export default {
  name: "Post",
  dicts: ['sys_normal_disable'],
  data() {
    return {
	  //审核信息
	  userStatusFrom:{
		  userStatus:0,
		  userId:''
	  },
	  //所有楼宇
	  ListFloorArr:[],
	 //所有房间
	  RoomList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roomId: undefined,
        floorId: undefined,
        userStatus: undefined,
        userType: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userName: [
          { required: true, message: "住户姓名不能为空", trigger: "blur" }
        ],
		phone: [
		  { required: true, message: "住户手机号不能为空", trigger: "blur" }
		],
        idCard: [
          { required: true, message: "住户身份证号不能为空", trigger: "blur" }
        ],
        roomId: [
          { required: true, message: "居住房间不能为空", trigger: "blur" }
        ],
        userStatus: [
          { required: true, message: "住户状态不能为空", trigger: "blur" }
        ],
        userType: [
          { required: true, message: "住户类型不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
	this.getRoomList()
	this.getListFloorArr()
  },
  computed: {
    peopleUserStatus: {
      get() {
        return this.$store.state.common.peopleUserStatus
      },
    },
	peopleUserType: {
	  get() {
	    return this.$store.state.common.peopleUserType
	  },
	}
  },
  methods: {
	  //获取所有房间
	  getRoomList(){
	  	this.$store.dispatch('AllRoomList').then(res => {
	  		 this.RoomList = res
			 this.getList();
	  	})
	  },
	  //获取所有楼宇
	  getListFloorArr(){
	  	this.$store.dispatch('AllFloorList').then(res => {
	  		 this.ListFloorArr = res
	  	})
	  },
    /** 查询住户列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
        this.postList = response.rows.map(i=>{
			let room = this.RoomList.filter(v=>v.id == i.roomId)[0]
			i.userTypeCn = this.peopleUserType.filter(v=>v.type == i.userType)[0].name
			i.userStatusCn = this.peopleUserStatus.filter(v=>v.type == i.userStatus)[0].name
			console.log(room)
			i.room = room.communityName +''+ room.floorName +''+ room.units +'单元'+ room.roomNo +'号'
			return i
		})
        this.total = response.total;
        this.loading = false;
      });
    },
	//审核按钮点击
	handleStatusUpdate(res){
		this.form = res
		this.userStatusFrom = {
			userId:res.id,
			userStatus:0
		}
		this.open = true
		this.title = '住户审核'
	},
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        communityId: undefined,
        floorName: undefined,
        units: undefined,
        layers: undefined,
        houseNum: undefined
      };
	  this.userStatusFrom.userId = null
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.postId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加住户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const postId = row.id
      this.form = row;
      this.open = true;
      this.title = "修改住户";
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
		  if(this.userStatusFrom.userId){
			  Examine(this.userStatusFrom).then(response => {
			    this.$modal.msgSuccess("审核成功");
			    this.open = false;
			    this.getList();
			  });
		  }else{
			  if (this.form.id != undefined) {
			    editUser(this.form).then(response => {
			      this.$modal.msgSuccess("修改成功");
			      this.open = false;
			      this.getList();
			    });
			  } else {
			    insertUser(this.form).then(response => {
			      this.$modal.msgSuccess("新增成功");
			      this.open = false;
			      this.getList();
			    });
			  }
		  }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const postIds = {
		  id:row.id
	  };
      this.$modal.confirm('是否确认该用户？').then(function() {
        return delUser(postIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>